package com.ncloud.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.common.enmus.ResultEnum;
import com.ncloud.common.exception.UserException;
import com.ncloud.system.entity.Menu;
import com.ncloud.system.mapper.MenuMapper;
import com.ncloud.system.service.MenuService;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

/**
 * Created by shiqiang on 2017/7/28.
 */
@Service
public class MenuServiceImpl implements MenuService{
    private final org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    private MenuMapper menuMapper;
    @Override
    public int insertMenu(Menu menu) {
        if(menu.getMenuId() == null){
            //获取
        }
        if(menu.getCreateDate()==null){
            menu.setCreateDate(new Date());
        }
        if(StringUtils.isEmpty(menu.getCreateUser())){
            throw new UserException(7001,"创建用户不能为空！{createUser}");
        }
        menu.setUseTag("1");
        return menuMapper.insertMenu(menu);
    }

    @Override
    public int updateMenu(Menu menu) {
        return menuMapper.updateMenu(menu);
    }

    @Override
    public Menu getOneMenu(Long menuId) {
        return menuMapper.getOneMenu(menuId);
    }

    @Override
    public int getSameMenuName(Long parentId, String menuName) {
        return menuMapper.getSameMenuName(parentId,menuName);
    }

    @Override
    public int getSameMenuOrder(Long parentId, Integer sort) {
        return menuMapper.getSameMenuOrder(parentId,sort);
    }

    @Override
    public int updateMenuState(String useTag, Long menuId) {
        return menuMapper.updateMenuState(useTag,menuId);
    }

    @Override
    public List<Menu> getMenusByRole(Long userId) {
        return menuMapper.getMenusByRole(userId);
    }

    @Override
    @Transactional
    public void updateMenuSort(JSONObject json) {

        if(!json.containsKey("sortBy")){
            throw new UserException(ResultEnum.PARAM_ERROR.getCode(),"json字段属性{sortBy}不存在！");
        }else{
            Menu menu = JSON.parseObject(json.toJSONString(),Menu.class);
            String sortBy = json.getString("sortBy");
            if("up".equals(sortBy)){
                Integer oldSort = menu.getSort();
                Integer newSort = oldSort - 1;
               List<Menu> menus = menuMapper.getMenuBySort(menu.getParentId(),newSort);
               int m =  menuMapper.updateMenuSort(menu.getMenuId(), newSort);
               /*修改之前的记录为就记录*/
               menus.stream().forEach(ms->{
                   logger.info(JSON.toJSONString(ms));
                   menuMapper.updateMenuSort(ms.getMenuId(), oldSort);
               });

            }else if("down".equals(sortBy)){
                Integer oldSort = menu.getSort();
                Integer newSort = oldSort + 1;
                List<Menu> menus = menuMapper.getMenuBySort(menu.getParentId(),newSort);
                int m =  menuMapper.updateMenuSort(menu.getMenuId(), newSort);
               /*修改之前的记录为就记录*/
                menus.stream().forEach(ms->{
                    menuMapper.updateMenuSort(ms.getMenuId(), oldSort);
                });
            }
        }
    }

    @Override
    public int updateMenuSort(Long parentId, Integer newSort) {
        return menuMapper.updateMenuSort(parentId,newSort);
    }

    @Override
    public int deleteMenu(Long menuId) {
        return menuMapper.deleteMenu(menuId);
    }

    @Override
    public List<Menu> getMenusByLevel(Long parentId) {
        return menuMapper.getMenusByLevel(parentId);
    }

    @Override
    public List<Menu> getMenusByUser(Long userId) {
        return menuMapper.getMenusByUser(userId);
    }

    @Override
    public List<Menu> getMenusAll() {
        return menuMapper.getMenusAll();
    }
}
